package com.geekydoo.commands;

import generated.TaskType;

import java.util.List;
import java.util.logging.Logger;

import com.geekydoo.InputOutputHandler;
import com.geekydoo.InputValidation;
import com.rits.cloning.Cloner;

/**
 * The purpose of this class is to provide support for deleting all tasks in the
 * program's task list.
 * 
 * @author Wai Yau
 */
public class ClearCommand implements Command {

	private static Logger logger = Logger.getLogger("geekydoo");

	// The list of all task objects managed by the program
	private List<TaskType> taskList;

	// List of all tasks as it was before the task list was cleared
	private static List<TaskType> preExecutionList;

	// Indicates whether or not the task list was cleared
	private boolean clearExecuted = false;

	// Valid user input strings
	private static final String INPUT_YES = "((?i)yes)|((?i)y)";
	private static final String INPUT_NO = "((?i)no)|((?i)n)";

	// Prompts printed by the clear command
	private static final String PROMPT_SURE = "Are you sure? (y/n): ";

	// Result information messages
	private static final String INFO_CLEAR = "Your task list has been cleared.";
	private static final String INFO_NOT_CLEAR = "You chose not to clear your task list.";

	public void execute(List<TaskType> tasks) {
		taskList = tasks;
		Cloner cloner = new Cloner();
		preExecutionList = cloner.deepClone(taskList);

		String answer = InputOutputHandler.getInputFromConsole(PROMPT_SURE, true);
		while (!InputValidation.isValidYesNoResponse(answer)) {
			answer = InputOutputHandler.getInputFromConsole(PROMPT_SURE, true);
		}

		processResponse(answer);
	}

	/**
	 * This method clears the task list if the response argument is 'y' or
	 * informs the user that the task list was not cleared if the response
	 * argument is 'n'.
	 * 
	 * @param response User input
	 */
	private void processResponse(String response) {
		if (response.matches(INPUT_YES)) {
			taskList.clear();
			clearExecuted = true;
			logger.info("User has cleared the task list");
			assert (taskList.size() == 0);
			InputOutputHandler.printToConsole(INFO_CLEAR, true, false);
		}
		else if (response.matches(INPUT_NO)) {
			logger.info("User has declined to clear the task list");
			InputOutputHandler.printToConsole(INFO_NOT_CLEAR, true, false);
		}
	}

	public boolean isListModified() {
		return clearExecuted;
	}

	public List<TaskType> getPreExecutionList() {
		return preExecutionList;
	}

}
